<?php

// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author yangweijie <yangweijiester@gmail.com>
 */
class CenterController extends AdminController {

    /**
     * 后台菜单首页
     * @return none
     */
    public function index() {
        $pid = I('get.pid', 0);
//        if ($pid) {
//            $data = M('GoodsCate')->where("pid={$pid}")->field(true)->find();
//            $this->assign('data', $data);
//        }
        $title = trim(I('get.title'));
        $map['pid'] = $pid;
        if ($title)
            $map['center_name'] = array('like', "%{$title}%");
        $list = M("Center")->where($map)->field(true)->order('sort asc,center_id asc')->select();
        if ($list) {

            $this->assign('list', $list);
        }
        // 记录当前列表页的cookie
        $this->meta_title = '中心列表';
        $this->display();
    }

    /**
     * 新增菜单
     * @author yangweijie <yangweijiester@gmail.com>
     */
    public function add() {
        $center_id = I('id', 0, 'intval');

        if (IS_POST) {
            $pid = I('pid', 0, 'intval');
            $title = I('title');

            $icon = I('icon');
            if (empty($title)) {
                $this->error('请输入中心名');
            }

            $data['center_icon'] = $icon;

            $data['pid'] = $pid;
            $data['center_name'] = $title;

            if ($center_id == 0) {
                $res = M('Center')->add($data);
            } else {
                $res = M('Center')->where(['center_id' => $center_id])->save($data);
            }
            if ($res) {
                $this->success('新增成功',U('index'));
            } else {
                $this->error('新增失败');
            }
        } else {


            if ($center_id != 0) {
                $info = M('Center')->where(['center_id' => $center_id])->find();
            }
            $this->assign('info', $info);
            $this->meta_title = '新增中心';
            $this->display('edit');
        }
    }

    /**
     * 删除后台菜单
     * @author yangweijie <yangweijiester@gmail.com>
     */
    public function del() {
        $id = array_unique((array) I('id', 0));

        if (empty($id)) {
            $this->error('请选择要操作的数据!');
        }

        $map = array('cate_id' => array('in', $id));
        if (M('GoodsCate')->where($map)->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 菜单排序
     * @author huajie <banhuajie@163.com>
     */
    public function sort() {

        if (IS_GET) {
            $ids = I('get.ids');
//            $pid = I('get.pid');
            Cookie('__forward__', U('Center/index'));
            //获取排序的数据
//            $map = array('status' => array('gt', -1));
            if (!empty($ids)) {
                $map['center_id'] = array('in', $ids);
            }
            $list = M('Center')->where($map)->field('center_id,center_name')->order('sort asc,center_id asc')->select();

            $this->assign('list', $list);
            $this->meta_title = '中心排序';
            $this->display();
        } elseif (IS_POST) {
            $ids = I('post.ids');
            $ids = explode(',', $ids);
            foreach ($ids as $key => $value) {
                $res = M('Center')->where(array('center_id' => $value))->setField('sort', $key + 1);
            }
            if ($res !== false) {
                $this->success('排序成功!', U('index'));
            } else {
                $this->error('排序失败！');
            }
        } else {
            $this->error('非法请求！');
        }
    }

}
